
const HttpException = require('../exception/http.exception.js');
const User = require('../model/user')

/*  
关注控制器
*/


//添加关注：A（登录用户=>关注者）=>(某人：被关注者)
const followController = async (req, res, next) => {
    try {
        //00 A用户 登录验证=>authMiddleware
        //01 获取被关注者username(注意：唯一)
        const befollowerUsername = req.params.username
        //02 获取被关注者作者信息（依据username=>userB）
        const beFollower = await User.findOne({
            where: {
                username: befollowerUsername
            }
        })
        // console.log(beFollower);
        //被关注者信息：验证是否存在
        if (!beFollower) {
            throw new HttpException(404, '被关注的用户不存在', 'user with this username not found')
        }
        //03 获取关注者的email
        const followerEmail = req.user.email
        if (beFollower.email === followerEmail) {
            throw new HttpException(404, '登录用户不能自关注', '登录用户不能自关注')
        }
        //04 获取关注者的信息（依据email=>userA）
        const follower = await User.findByPk(followerEmail)  //粉丝 读者
        //05 添加关注：关注者（读者）给 被关注者（作者）添加关注（主动行为）
        // console.log(beFollower);
        console.log(beFollower.__proto__);
        // console.log(beFollower.__proto__);
        const result = await beFollower.addFollower(follower)
        console.log("123", result);
        //06 添加关注结果信息处理
        //字段少 而且需要追加字段=>方便
        const beFollowerProfile = {
            username: beFollower.username,
            bio: beFollower.bio,
            avatar: beFollower.avatar,
            following: true
        }
        //07 返回信息
        return res.status(200).json({
            status: 1,
            message: 'follow ok',
            data: beFollowerProfile
        })
    } catch (error) {
        next(error)
    }
}

//取消关注：A（登录用户=>关注者）=>(某人：被关注者)
const unfollowController = async (req, res, next) => {
    try {
        //00 A用户 登录验证=>authMiddleware
        //01 获取被关注者username(注意：唯一)
        const befollowerUsername = req.params.username
        //02 获取被关注者作者信息（依据username=>userB）
        const beFollower = await User.findOne({
            where: {
                username: befollowerUsername
            }
        })
        // console.log(beFollower);
        //被关注者信息：验证是否存在
        if (!beFollower) {
            throw new HttpException(404, '被关注的用户不存在', 'user with this username not found')
        }
        //03 获取关注者的email
        const followerEmail = req.user.email
        //04 获取关注者的信息（依据email=>userA）
        const follower = await User.findByPk(followerEmail)  //粉丝 读者
        //05 添加关注：关注者（读者）给 被关注者（作者）添加关注（主动行为）
        // console.log(beFollower);
        // console.log(beFollower.__proto__);
        // console.log(beFollower.__proto__);
        const result = await beFollower.removeFollower(follower)
        // console.log("123",result);
        //06 添加关注结果信息处理
        //字段少 而且需要追加字段=>方便
        const beFollowerProfile = {
            username: beFollower.username,
            bio: beFollower.bio,
            avatar: beFollower.avatar,
            following: true
        }
        //07 返回信息
        return res.status(200).json({
            status: 1,
            message: 'follow delete',
            data: beFollowerProfile
        })
    } catch (error) {
        next(error)
    }
}
//获取关注者XXX
//场景：用户主页（查看其他人用户信息）=>我是否关注他，看看他的粉丝
const followerController = async (req, res, next) => {
    //01 获取被关注者 用户名
    const username = req.params.username
    //02 获取被关注者作者信息（依据username=>userB）
    const beFollower = await User.findOne({
        where: {
            username: username
        },
        include: ['followers']  //通过followers中间表进行关联查询
    })
    // console.log("beFollower", beFollower);
    // console.log("beFollower", beFollower.followers[0]);
    //被关注者信息：验证是否存在
    if (!beFollower) {
        throw new HttpException(404, '被关注的用户不存在', 'user with this username not found')
    }
    //获取被关注者所有粉丝
    //01) 获取当前登录用户（关注者）email
    //02) 获取被关注者所有粉丝beFollower
    //当前登录是否关注beFollower
    const { email } = req.user
    let following = false     //当前登录用户是否关注的标志
    let followers = []        //粉丝信息处理的容器
    for (const follower of beFollower.followers) {
        if (email === follower.dataValues.email) {
            following = true
        }

        //粉丝处理
        delete follower.dataValues.password
        delete follower.dataValues.Followers

        followers.push(follower)
    }
    //返回被关注者信息
    //基本信息
    //关注信息
    //粉丝信息

    const profile = {
        username: beFollower.username,
        bio: beFollower.bio,
        avatar: beFollower.avatar,
        following,
        followers,

    }
    //查看是否被关注
    return res.status(200).json({
        status: 1,
        message: '获取关注信息成功',
        data: profile
    })
}



module.exports = { followController, unfollowController, followerController }




